package com.mjh.zhujianju.service.agency.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.mjh.zhujianju.util.FileUrlAdapter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;
import com.mjh.zhujianju.dao.agency.OrganizationMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.agency.OrganizationEO;
import com.mjh.zhujianju.model.base.ContentEO;
import com.mjh.zhujianju.service.agency.OrganizationService;
import com.mjh.zhujianju.service.base.AreasService;
import com.mjh.zhujianju.service.base.ContentService;
import com.mjh.zhujianju.vo.agency.OrganizationVO;

/**
 * @author liuyang
 * @Description: 机构
 * @date 2018/8/8 22:23
 */
@Service
public class OrganizationServiceImpl extends BaseServiceImpl<OrganizationEO> implements OrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;
    
    @Resource
    private FileService fileService;
    
    @Resource
    private ContentService contentService;
    
    @Resource
    private HttpServletRequest request;

    @Resource
    private AreasService areasService;

    public OrganizationServiceImpl(OrganizationMapper organizationMapper) {
        super(organizationMapper);
    }

	@Override
	public OrganizationVO getInfo() {
		List<OrganizationEO> list = organizationMapper.selectAll();
		if(XTListUtils.isEmpty(list)) {
			return null;
		}
		OrganizationEO eo =	list.get(0);
		OrganizationVO vo = new OrganizationVO();
		BeanUtils.copyProperties(eo, vo);
		List<Long> fileIds = fileService.getFileByCaseId(eo.getId(), OrganizationEO.class.getName(), null);
		if (!CollectionUtils.isEmpty(fileIds)){
			vo.setFileActions(FileUrlAdapter.assemblyAction(fileIds));
		}
		ContentEO content =	contentService.queryContentByTypeAndId(BusinessEnum.TYPE_ORGANIZATION.getCode(), vo.getId());
		if(content!=null) {
			vo.setContent(content.getContent());
		}
		return vo;
	}

	
	@Override
	public void updateByPrimaryKey(OrganizationEO record) throws XTBusinessException {
		if(record==null||XTStringUtils.isEmpty(record.getName())
				||XTStringUtils.isEmpty(record.getDetailAddress())) {
			throw new XTBusinessException("参数错误");
		}
		super.updateByPrimaryKey(record);
	}

	@Transactional
	@Override
	public void saveInfo(OrganizationVO vo) throws XTBusinessException {
		OrganizationEO org = new OrganizationEO();
		BeanUtils.copyProperties(vo, org);
		String content = vo.getContent();
		org.setContent(null);
		Map<String, String> map =	areasService.getMap(null,vo.getCityCode(),vo.getCountyCode(),vo.getStreetCode());
		if(map!=null) {
			org.setCityName(map.get(org.getCityCode()));
			org.setCountyName(map.get(org.getCountyCode()));
			org.setStreetName(map.get(org.getStreetCode()));
		}
		if(!XTStringUtils.isEmpty(vo.getRemark2())&&vo.getRemark2().contains(",")) {
			String[] latlon = vo.getRemark2().split(",");
			BigDecimal lat= new BigDecimal(latlon[0]);
			BigDecimal lon= new BigDecimal(latlon[1]);
			org.setLat(lat);
			org.setLon(lon);
		}
		this.save(org);
		if(!XTListUtils.isEmpty(vo.getFiles())) {
			fileService.updateList(vo.getFiles(), org.getId(), OrganizationEO.class.getName());
		}
		if(content!=null) {
			contentService.saveOrUpdate(BusinessEnum.TYPE_ORGANIZATION.getCode(), org.getId(), content);
		}
	}

	@Override
	public OrganizationEO get() {
		List<OrganizationEO> list = organizationMapper.selectAll();
		if(XTListUtils.isEmpty(list)) {
			return null;
		}
		OrganizationEO eo =	list.get(0);
		return eo;
	}

	@Override
	public void updateInfo(OrganizationVO vo) throws XTBusinessException {
		
		OrganizationEO org = new OrganizationEO();
		BeanUtils.copyProperties(vo, org);
		String content = vo.getContent();
		Map<String, String> map =	areasService.getMap(null,vo.getCityCode(),vo.getCountyCode(),vo.getStreetCode());
		if(map!=null) {
			org.setCityName(map.get(org.getCityCode()));
			org.setCountyName(map.get(org.getCountyCode()));
			org.setStreetName(map.get(org.getStreetCode()));
		}
		org.setContent(null);
		if(!XTStringUtils.isEmpty(vo.getRemark2())&&vo.getRemark2().contains(",")) {
			String[] latlon = vo.getRemark2().split(",");
			BigDecimal lat= new BigDecimal(latlon[0]);
			BigDecimal lon= new BigDecimal(latlon[1]);
			org.setLat(lat);
			org.setLon(lon);
		}
		this.updateByPrimaryKeySelective(org);
		if(!XTListUtils.isEmpty(vo.getFiles())) {
			fileService.updateList(vo.getFiles(), org.getId(), OrganizationEO.class.getName());
		}
		if(content!=null) {
			contentService.saveOrUpdate(BusinessEnum.TYPE_ORGANIZATION.getCode(), org.getId(), content);
		}	
		
	}
	
	


}
